
import OSS from "ali-oss";

//oss客户端
export function client() {
  var client = new OSS({
    endpoint: 'oss-cn-chengdu.aliyuncs.com',//填写Bucket所在地域
    accessKeyId: 'LTAI5tJLvbpa3mtW1yW5rRmd',
    accessKeySecret: 'y8rJAGo9H0DA1MaE14KVE0BSMfe2hk',
    bucket: 'aliyun-shelly-oss',// 填写Bucket名称。
  })  //后端提供数据
  return client
}

//生成uuid
export const getFileNameUUID = () => {
  function rx() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
  }
  return `${+new Date()}_${rx()}${rx()}`
}

//上传oss专利图标
export function uploadImg(file) {
  return new Promise((resolve, reject) => {
    var fileName = 'ipms/' + 'img' + `${Date.parse(new Date())}` + '.jpg'
    client()
      .multipartUpload(fileName, file, {
        progress: function (percentage, cpt) {
          console.log('图片上传进度', percentage)
        },
      })
      .then((res) => {
        resolve('https://aliyun-shelly-oss.oss-cn-chengdu.aliyuncs.com/' + fileName);
      }).catch(err => {
        reject(err)
      })
  });
}

//上传oss专利资料包文件
export function uploadFile(file) {

  return new Promise((resolve, reject) => {
    // 获取文件类型
    let fileType = file.name.split('.')[1];
    // 制作文件名
    // let fileName = new Date().getTime() + Math.floor(Math.random() * 150) + '.' + fileType;
    let fileName = 'ipms/' + 'file' + `${Date.parse(new Date())}` + '.' + fileType;
    // 上传
    client().multipartUpload(fileName, file).then(result => {
      resolve(result.res.requestUrls[0].split('?')[0])
    }).catch(err => {
      reject(err)
    })
  })
}
